﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;
using System.Data.Linq;

namespace Avatar.ResumeService
{
    public static class ResumeManager
    {
        /// <summary>
        /// 获取指定用户简历
        /// </summary>
        /// <param name="_userName"></param>
        /// <returns></returns>
        public static Resume GetResume(string _userName)
        {
            return DBHelper.Get<Resume>(r=>r.User==_userName,"获取制定用户简历出错:");
        }
        /// <summary>
        /// 新用户简历数据初始化
        /// </summary>
        /// <param name="_userName"></param>
        /// <returns></returns>
        public static void Initialization(string _userName)
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_userName)) throw new Exception("用户名不能为空");

                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = new Resume();
                    _resume.User = _userName;
                    _resume.ViewCount = 0;

                    _adc.Resume.InsertOnSubmit(_resume);
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("新用户简历数据初始化错误:" + _e.Message);
            }
        }

        /// <summary>
        /// 检查用户是否填写了基本资料
        /// </summary>
        /// <param name="_userName"></param>
        public static void PersonResumeBasicInformationCheck(string _userName)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _resume = _adc.Resume.Where(r => r.User == _userName).FirstOrDefault();

                if (_resume.PersonalBasicInfo == null)
                {
                    throw new Exception("基本资料未填写，请先填写这部分信息。");
                }
            }
        }
        /// <summary>
        /// 检查用户是否填写了求职意向
        /// </summary>
        /// <param name="_userName"></param>
        /// <returns></returns>
        public static void PersonResumeJobIntentionCheck(string _userName)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _resume = _adc.Resume.Where(r => r.User == _userName).FirstOrDefault();

                if (_resume.JobIntention == null)
                {
                    throw new Exception("求职意向未填写，请先填写这部分信息。");
                }
            }
        }
        /// <summary>
        /// 统计系统个人用户简历收录总数
        /// </summary>
        /// <returns></returns>
        public static int ResumeCount()
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                return _adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null);
            }
        }
    }
}
